using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;

namespace CircleRun
{
    public class CircleRunScene : CCLayer
    {
        public Track m_Track;
        public Player m_PlayerA;
        public Player m_PlayerB;

        public static double DegreeToRadians(double fdegree)
        {
            return fdegree * Math.PI / 180.0; 
        }

        public override bool init()
        {
            CCDirector.sharedDirector().deviceOrientation = ccDeviceOrientation.CCDeviceOrientationLandscapeLeft;

            //////////////////////////////
            // 1. super init first
            if (!base.init())
            {
                return false;
            }

            this.m_bIsTouchEnabled = true;
            /////////////////////////////
            // 2. add a menu item with "X" image, which is clicked to quit the program
            //    you may modify it.

            // add a "close" icon to exit the progress. it's an autorelease object
            CCMenuItemImage pCloseItem = CCMenuItemImage.itemFromNormalImage(
                                                "CloseNormal",
                                                "CloseSelected",
                                                this,
                                                new SEL_MenuHandler(menuCloseCallback));
            pCloseItem.position = new CCPoint(CCDirector.sharedDirector().getWinSize().width - 20, 20);

            // create menu, it's an autorelease object
            CCMenu pMenu = CCMenu.menuWithItems(pCloseItem);
            pMenu.position = new CCPoint(0, 0);
            this.addChild(pMenu, 1);

            /////////////////////////////


            //game init
            CCPoint ccenter = new CCPoint(0.0f, 0.0f);
            float cradius = 50.0f;
            //1 track
            m_Track = new Track();
            m_Track.InitTrackData(ccenter, cradius);
            //2 player
            m_PlayerA = new Player("Billy");
            m_PlayerB = new Player("Jimmy");
            CCPoint playerAStartPos = new CCPoint(ccenter.x, ccenter.y + cradius);
            CCPoint playerBStartPos = new CCPoint(ccenter.x, ccenter.y - cradius);
            m_PlayerA.rotateByPoint(playerAStartPos, 0.0f, ccenter);
            m_PlayerB.rotateByPoint(playerBStartPos,180.0f,ccenter);
            //
            this.addChild(m_Track,0);


            return true;
        }

        /// <summary>
        /// there's no 'id' in cpp, so we recommand to return the exactly class pointer
        /// </summary>
        public static CCScene scene()
        {
            // 'scene' is an autorelease object
            CCScene scene = CCScene.node();

            // 'layer' is an autorelease object
            CCLayer layer = CircleRunScene.node();

            // add layer as a child to scene
            scene.addChild(layer);

            // return the scene
            return scene;
        }

        public static new CCLayer node()
        {
            CircleRunScene ret = new CircleRunScene();
            if (ret.init())
            {
                return ret;
            }
            else
            {
                ret = null;
            }

            return ret;
        }

        /// <summary>
        /// a selector callback
        /// </summary>
        /// <param name="pSender"></param>
        public virtual void menuCloseCallback(CCObject pSender)
        {
            CCDirector.sharedDirector().end();
            CCApplication.sharedApplication().Game.Exit();
        }


    }
}
